home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / paul_qureshi / source / fnevlcpp.lha / FuncEval.hpp < prev    next >
C/C++ Source or Header  |  1995-02-12  |  4KB  |  143 lines

  1. /*****************************************
  2. *                                        *
  3. *  FuncEval.hpp                          *
  4. *                                        *
  5. *  Definition file for FuncEval class.   *
  6. *                                        *
  7. *  Implementation in FuncEval.cpp.       *
  8. *                                        *
  9. ******************************************
  10. *                                        *
  11. *  Copyright 1995  Randy C. Finch        *
  12. *                                        *
  13. *****************************************/
  14.  
  15. #ifndef CLASS_FUNCEVAL
  16. #define CLASS_FUNCEVAL
  17.  
  18.  
  19. /*--------------  INCLUDES  ----------------*/
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include <float.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27.  
  28.  
  29. /*----------------  DEFINES  -----------------*/
  30.  
  31. /* Variable Types */
  32.  
  33. typedef unsigned char UCHAR;
  34. typedef unsigned int  UINT;
  35. typedef unsigned long ULONG;
  36.  
  37. /* Errors */
  38.  
  39. #define MISPLACEDOP     1    /* Misplaced operator */
  40. #define ILLEGALCHAR     2    /* Illegal character */
  41. #define ILLEGALEXP      3    /* Illegal exponent */
  42. #define ILLEGALFUNC     4    /* Illegal function */
  43. #define MISSINGOP       5    /* Missing operator */
  44. #define MISSINGOPRP     6    /* Missing operator or right parenthesis */
  45. #define MISSINGLP       7    /* Missing left parenthesis */
  46. #define MISSINGRP       8    /* Missing right parenthesis */
  47. #define MISSINGPARM     9    /* Missing parameter */
  48. #define LONEDECIMAL    10    /* Lone decimal point */
  49. #define EXTRADECIMAL   11    /* Extra decimal point */
  50. #define EXTRAE         12    /* Extra E in exponent */
  51. #define STACKUNDERFLOW 13    /* Stack underflow */
  52. #define STACKOVERFLOW  14    /* Stack overflow */
  53. #define TOOMANYCONST   15    /* Too many constants in function */
  54.  
  55. /* Constants in formula */
  56.  
  57. #define NUMSYM    128  /* Number of constants allowed in function */
  58. #define SYMBASE   128  /* Base value for constants symbols */
  59.  
  60. /* Stack size */
  61.  
  62. #define STACKSIZE 256  /* Stack size */
  63.  
  64. /* Functions in formula */
  65.  
  66. #define SIN         1  /* Symbol for sine */
  67. #define COS         2  /* Symbol for cosine */
  68. #define TAN         3  /* Symbol for tangent */
  69. #define ASIN        4  /* Symbol for arcsine */
  70. #define ACOS        5  /* Symbol for arccosine */
  71. #define ATAN        6  /* Symbol for arctangent */
  72. #define SINH        7  /* Symbol for hyperbolic sine */
  73. #define COSH        8  /* Symbol for hyperbolic cosine */
  74. #define TANH        9  /* Symbol for hyperbolic tangent */
  75. #define EXP        10  /* Symbol for exponential */
  76. #define SQRT       11  /* Symbol for square root */
  77. #define LN         12  /* Symbol for natural logarithm */
  78. #define LOG        13  /* Symbol for logarithn base 10 */
  79.  
  80. /* Logical stuff */
  81.  
  82. #define TRUE        1  /* Symbol for true condition */
  83. #define FALSE       0  /* Symbol for false condition */
  84.  
  85.  
  86. /*----------- STRUCTURE DEFINITIONS ----------*/
  87.  
  88. struct CharStackType {
  89.    UCHAR c[STACKSIZE];
  90.    long          top;
  91. };
  92. typedef struct CharStackType CharStack;
  93.  
  94. struct NumStackType {
  95.    double n[STACKSIZE];
  96.    long   top;
  97. };
  98. typedef struct NumStackType NumStack;
  99.  
  100.  
  101. /*------------ CLASS DEFINITION -------------*/
  102.  
  103. class FuncEval {
  104. public:
  105.    FuncEval();  // Constructor
  106.    double Evaluate(double x, double y);
  107.    int Convert(UCHAR *FunctionString);
  108.    int GetSyntaxErrNum();
  109.    char *GetSyntaxErrMessage();
  110.    int GetErrPosition();
  111. private:
  112.    int SyntaxErr;
  113.    char *ErrMessage;
  114.    int ErrPosition;
  115.    CharStack cstack;
  116.    NumStack nstack;
  117.    double Constants[NUMSYM];
  118.    UCHAR CurConstant;
  119.    UCHAR fstr[512];
  120.    UCHAR NewExpr[256];
  121.  
  122.    char CharInStr(UCHAR *s, UCHAR c);
  123.    void Deposit(double num);
  124.    void Substitute(UCHAR symb, UCHAR *ptr, ULONG len);
  125.    void RemoveSpaces(UCHAR *str);
  126.    void AddZero(UCHAR *ptr);
  127.    UCHAR CPop();
  128.    char CPush(UCHAR c);
  129.    UCHAR CTopOfStack();
  130.    double NPop();
  131.    void NPush(double n);
  132.    char IsFunction(UCHAR c);
  133.    char IsSymbol(UCHAR c);
  134.    char Precedence(UCHAR c1, UCHAR c2);
  135.    UCHAR *CheckSyntax(UCHAR *str);
  136.    char ConvertConstants(UCHAR *str);
  137.    void ConvertFunctions(UCHAR *str);
  138.    char InfixToPostfix(UCHAR *str);
  139.    double Calculate(UCHAR s, double n2, double n1);
  140. };
  141.  
  142. #endif
  143.